<template>
  <div class="postMana-container">
    <div class="posMana-header">
      <el-input size="small" class="addPosInput" placeholder="添加职位.." suffix-icon="el-icon-plus" @keydown.enter.native="addPisition" v-model="pos.name"> </el-input>
      <el-button icon="el-icon-plus" size="small" type="primary" @click="addPisition">添加</el-button>
      <el-button size="small" type="danger" :disabled="this.multipleSelection.length == 0" @click="deleteMany">批量删除</el-button>
    </div>
    <div class="posMana-body">
      <el-table size="small" :data="positions" stripe border style="width: 70%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="编号" width="100"> </el-table-column>
        <el-table-column prop="name" label="职位" width="120"> </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="200"> </el-table-column>
        <el-table-column prop="enabled" label="是否启用" width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="showEditView(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 编辑弹出框 -->
    <el-dialog title="编辑职位" :visible.sync="dialogVisible" width="30%">
      <table>
        <tr>
          <td><el-tag>职位名称</el-tag></td>
          <td>
            <el-input v-model="updatePos.name" size="small" class="updatePosInput"></el-input>
          </td>
        </tr>
        <!-- <tr>
          <td><el-tag>是否启用</el-tag></td>
          <td><el-switch style="margin-left: 6px" v-model="updatePos.enabled" active-color="#13ce66" inactive-color="#ff4949" active-text="已启用" inactive-text="未启用"> </el-switch></td>
        </tr> -->
      </table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'PostMana',
  created() {
    this.initPositions()
  },
  data() {
    return {
      pos: {},
      positions: [],
      dialogVisible: false,
      updatePos: {
        name: '',
      },
      multipleSelection: [], //批量删除列表
    }
  },
  methods: {
    async initPositions() {
      const data = await this.getRequest('/system/basic/pos/')
      if (data) {
        this.positions = data
        console.log(data)
      }
    },
    async addPisition() {
      if (this.pos.name) {
        const data = await this.postRequest('/system/basic/pos/', this.pos)
        if (data) {
          this.initPositions()
        }
      } else {
        this.$message.error('职位名称不能为空')
      }
    },
    //监听多选
    handleSelectionChange(selections) {
      this.multipleSelection = selections
      // console.log(this.multipleSelection)
    },
    showEditView(index, position) {
      // this.updatePos = position 这里会出现问题是 当我修改编辑弹出框的职位名称时，表格里的数据也会同步修改 因为updatePos和表格里的position此时引用相同
      Object.assign(this.updatePos, position)
      //createdate要置空 否则有问题
      this.updatePos.createDate = ''
      this.dialogVisible = true
    },
    handleDelete(index, position) {
      this.$confirm('此操作将永久删除 [' + position.name + '] 职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.deleteRequest('/system/basic/pos/' + position.id).then((resp) => {
            if (resp) {
              this.initPositions()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    async doUpdate() {
      const data = await this.putRequest('/system/basic/pos/', this.updatePos)
      if (data) {
        this.initPositions()
        this.dialogVisible = false
      }
    },
    //批量删除
    deleteMany() {
      this.$confirm('此操作将删[' + this.multipleSelection.length + ']条职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          let ids = '?'
          this.multipleSelection.forEach((item) => {
            ids += 'ids=' + item.id + '&'
          })
          // console.log(ids)
          // let ids = []
          // this.multipleSelection.forEach((item) => {
          //   ids.push(item.id)
          // })
          this.deleteRequest('/system/basic/pos/?' + ids).then((resp) => {
            if (resp) {
              this.initPositions()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
  },
}
</script>

<style lang="less" scoped>
.addPosInput {
  width: 350px;
  margin: 0 10px 10px 0;
}

.el-dialog {
  .updatePosInput {
    // width: 200px;
    margin-left: 8px;
  }
  // table {
  //   width: 100%;
  //   tr {
  //     display: flex;
  //     margin: 10px;
  //     td {
  //       &:first-child {
  //         flex: 20%;
  //       }
  //       &:last-child {
  //         flex: 80%;
  //       }
  //     }
  //   }
  // }
}
</style>
